package assignments.sagarjoshi.springtms;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SearchView;
import android.widget.Toast;


public class Activity_AdminHome extends Activity implements ListContainer.OnFragmentInteractionListener, SearchView.OnQueryTextListener
{

    boolean flag=false;
    SearchView srchbox;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin_home);

        // To redirect a person to login if directly tried to launch activity explicitly
        SharedPreferences preferences= getSharedPreferences("session",this.MODE_PRIVATE);
        String role=preferences.getString("role","");
        if(!role.equals("admin"))
        {
            startActivity(new Intent(this,LoginScreen.class));
            this.finish();
        }
        FragmentManager mgr= getFragmentManager();

        recreatelist("");

        srchbox=(SearchView)findViewById(R.id.srchbox);
        srchbox.setOnQueryTextListener(this);

    }

    protected void onResume()
    {
        super.onResume();
        recreatelist("");
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_admin_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_logout)
        {
            // Remove Session
            SharedPreferences.Editor editor = getSharedPreferences("session",this.MODE_PRIVATE).edit();
            editor.putString("email_id","");
            editor.putString("role","");
            editor.commit();
            startActivity(new Intent(this,LoginScreen.class));
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFragmentItemClicked(int item_id)
    {
            Intent i;
            if(!flag)
            {
                 i = new Intent(this,Activity_Allocation.class);
            }
           else
            {
                 i= new Intent(this,NeworEditTrainee.class);
            }
        i.putExtra("item_id",item_id);
        startActivity(i);
    }



    public boolean onQueryTextSubmit(String query)
    {

        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText)
    {
        recreatelist(newText);

        return false;
    }

    private void recreatelist(String query)
    {
        if(!flag)
            getActionBar().setTitle(R.string.courses);
        else
            getActionBar().setTitle(R.string.trainees);
        Bundle b= new Bundle();
        b.putString("query",query);
        b.putBoolean("flag",flag);
        ListContainer container = new ListContainer();

        container.setArguments(b);

        getFragmentManager().beginTransaction().replace(R.id.listframe,container).commit();
    }
    public void onButtonClick(View v)
    {
        int id=v.getId();
        if(id==R.id.btn_chnglist)
        {
            if(!flag)
                ((Button)v).setText(R.string.courses);
            else
                ((Button)v).setText(R.string.trainees);

            flag=!flag;
            //Check whether button click works without replacing fragment explicitly
            ((SearchView)findViewById(R.id.srchbox)).setQuery("", false);
            recreatelist("");



        }

        if(id==R.id.btn_add)
        {
            if(!flag)
            {
                startActivity(new Intent(this,AddorEditCourse.class));
            }
           else
           {
               startActivity(new Intent(this,NeworEditTrainee.class));
           }
        }

    }




}
